VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "clsSendEMail"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit

Public Sub SendErrorEmail(strErrorMsg As String)

Dim cJMail As clsJMail
Dim strEMailBody As String
Dim i As Integer

    On Error GoTo SendErrorEmailFix

    If TimeToEmail Then
    
        Set cJMail = New clsJMail
        
        ' Email Body
        strEMailBody = "Please be advised . . " & vbCrLf & vbCrLf
        strEMailBody = strEMailBody & "The following error occurred in the KeyTax Interface (ReceiveXml):" & vbCrLf
        strEMailBody = strEMailBody & strErrorMsg
        
        'Get mail settings
        cJMail.EMailServer = "trend1.liberty.co.za"
        cJMail.MsgFrom = "KtInterface.Receive@liberty.co.za"
        cJMail.MSGFromName = "KeyTax Interface"
        cJMail.MSGSubject = "KeyTax Interface"
        cJMail.MSGBody = strEMailBody
        
'        'Get recipients
'        i = 1
'        While objReg.GetEMailAddress(i) <> ""
'            cJMail.AddRecipient objReg.GetEMailAddress(i), objReg.GetEMailAddress(i)
'            i = i + 1
'        Wend

        'for testing
        'Fikile: commented this out: 2008-04-23
        cJMail.AddRecipient "Johann@mindkey.co.za", "Johann@mindkey.co.za"
        'cJMail.AddRecipient "tshepiso@mindkey.co.za", "tshepiso@mindkey.co.za"
        cJMail.AddRecipient "fikile@mindkey.co.za", "fikile@mindkey.co.za"
        cJMail.AddRecipient "gerrit.ferreira@liberty.co.za", "gerrit.ferreira@liberty.co.za"
        
        'Send Mail
        If Not cJMail.SendMSG Then
            'send email to one of us if we're not already on the distr list
            cJMail.EMailServer = "trend1.liberty.co.za"
            cJMail.MsgFrom = "KtInterface.Receive@liberty.co.za"
            cJMail.MSGFromName = "KeyTax Interface"
            cJMail.MSGSubject = "KeyTax Interface Emailing Error"
            cJMail.MSGBody = "Error: SendErrorEmail failed. Trying to send error message: " & vbCrLf & strErrorMsg
            cJMail.AddRecipient "Johann@mindkey.co.za", "Johann@mindkey.co.za"
            'cJMail.AddRecipient "tshepiso@mindkey.co.za", "tshepiso@mindkey.co.za"
            cJMail.AddRecipient "fikile@mindkey.co.za", "fikile@mindkey.co.za"
            cJMail.AddRecipient "gerrit.ferreira@liberty.co.za", "gerrit.ferreira@liberty.co.za"
            cJMail.SendMSG

        Else
            objReg.LastEmailDttm = Now
            'MsgBox "Sent Mail"
        End If
        
        ' Cleanup
        Set cJMail = Nothing
    
    End If
 
    Exit Sub
    
SendErrorEmailFix:
    AddErrorMessage ("SendErrorEmail : " & Err.Number & " - " & Err.Description)

End Sub
Public Sub SendLogEmail()

'   This function sends the log file via email to everyone in
'   the distribution list after each run

    Dim cJMail As clsJMail
    Dim strEMailBody As String
    Dim i As Integer
    Dim strLogFileName As String

    On Error GoTo SendLogEmailFix

    'If TimeToEmail Then
    'check that some files were processed
    If lngIdxFileCount > 0 Then
    
        Set cJMail = New clsJMail
        
        'barb: startr here = this won't work if you're not writing to the log file - git!!
        strLogFileName = objReg.LogFilePath & strCurrentLogFileName
        If Dir(strLogFileName) = "" Then
            GoTo ErrorAlert
        End If
        
        ' Email Body
        strEMailBody = "KeyTax Interface Log file: " & strLogFileName & vbCrLf
        strEMailBody = strEMailBody & "for batch run on: " & objReg.LastRunDttm
        
        'Get mail settings
        cJMail.EMailServer = "trend1.liberty.co.za"
        cJMail.MsgFrom = "KtInterface.Receive@liberty.co.za"
        cJMail.MSGFromName = "KeyTax Interface"
        cJMail.MSGSubject = "KeyTax Interface Log"
        cJMail.MSGBody = strEMailBody
        
'        'Get recipients
'        i = 1
'        While objReg.GetEMailAddress(i) <> ""
'            cJMail.AddRecipient objReg.GetEMailAddress(i), objReg.GetEMailAddress(i)
'            i = i + 1
'        Wend
        
        'for testing
        cJMail.AddRecipient "Johann@mindkey.co.za", "Johann@mindkey.co.za"
        'cJMail.AddRecipient "tshepiso@mindkey.co.za", "tshepiso@mindkey.co.za"
        cJMail.AddRecipient "fikile@mindkey.co.za", "fikile@mindkey.co.za"
        cJMail.AddRecipient "gerrit.ferreira@liberty.co.za", "gerrit.ferreira@liberty.co.za"
        'Add the log file as an attachemnt
        cJMail.AddAttachment strLogFileName
            
        'Send Mail
        If Not cJMail.SendMSG Then
ErrorAlert:
            'send email to one of us if we're not already on the distr list
            cJMail.EMailServer = "trend1.liberty.co.za"
            cJMail.MsgFrom = "KtInterface.Receive@liberty.co.za"
            cJMail.MSGFromName = "KeyTax Interface"
            cJMail.MSGSubject = "KeyTax Interface Emailing Error"
            cJMail.MSGBody = "Error: SendLogEmail failed. Trying to send log file: " & vbCrLf & strLogFileName
            cJMail.AddRecipient "Johann@mindkey.co.za", "Johann@mindkey.co.za"
            'cJMail.AddRecipient "tshepiso@mindkey.co.za", "tshepiso@mindkey.co.za"
            cJMail.AddRecipient "fikile@mindkey.co.za", "fikile@mindkey.co.za"
            cJMail.AddRecipient "gerrit.ferreira@liberty.co.za", "gerrit.ferreira@liberty.co.za"
            cJMail.SendMSG
        End If
        
        ' Cleanup
        Set cJMail = Nothing
    
    End If
 
    Exit Sub
    
SendLogEmailFix:
    AddErrorMessage ("SendLogEmail : " & Err.Number & " - " & Err.Description)
End Sub
Private Function TimeToEmail() As Boolean
         
    If LeftPad(DatePart("h", Now), "0", 2) & LeftPad(DatePart("n", Now), "0", 2) >= objReg.GetDayStart(DatePart("w", Now)) _
    And LeftPad(DatePart("h", Now), "0", 2) & LeftPad(DatePart("n", Now), "0", 2) <= objReg.GetDayEnd(DatePart("w", Now)) Then
        If Abs(DateDiff("n", CDate(objReg.LastEmailDttm), CDate(Now))) >= 60 Then
            TimeToEmail = True
        Else
            TimeToEmail = False
        End If
    Else
        TimeToEmail = False
    End If
    
    TimeToEmail = True
    
End Function

